package com.azt.provider.impl;
import com.azt.api.pojo.Channel;
import com.azt.api.pojo.Content;
import com.azt.api.service.ContentService;
import com.azt.exception.BaseException;
import com.azt.model.page.Pagination;
import com.azt.provider.mapper.ChannelMapper;
import com.azt.provider.mapper.ContentMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xiaoleilu.hutool.util.CollectionUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ContentServiceImpl  implements ContentService {

	@Autowired
	private ContentMapper cm;
	
	@Autowired
	private ChannelMapper channelMapper;

	@Override
	public List<Content> findIndexNews(String code, int pageSize) {
		return cm.findIndexNews(code, pageSize);
	}

	@Override
	public List<Content> findHotTips(String code, int pageSize) {
		return cm.findHotTips(code, pageSize);
	}

	@Override
	public Pagination getContentPage(int pageno, int pagesize, Map<String, String> searchMap) {
		Page<Object> startPage = PageHelper.startPage(pageno, pagesize);
		List<Content> contentList = cm.searchContent(searchMap);
		Pagination p = new Pagination(startPage, contentList);
		p.setList(contentList);
		p.setParams(searchMap);
		return p;
	}

	@Override
	public Content getContentById(Integer id) {
		Content c=cm.selectByPrimaryKey(id);
		if(c != null) {
			Channel cha = channelMapper.selectByPrimaryKey(c.getChannelId());
			c.setChannel(cha);
		}
		return c;
	}

	@Override
	public void saveOrUpdateContent(Content content) {
		if(content.getId() == null) cm.insert(content);
		else cm.updateByPrimaryKey(content);
	}

	@Override
	public void deleteContent(Content content) {
		if(content != null) {
			content.setIfImg(null);
			content.setIfUseable(null);
			content.setHit(null);
			cm.delete(content);
		}
	}

	@Override
	public void deleteByIds(String ids) {
		if (StringUtils.isBlank(ids)) {
			throw new BaseException("请传入要删除的 ID");
		}
		cm.deleteByIds(StringUtils.split(ids, ","));
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Content> getRecommendContents(Integer num) {
		Example exp = new Example(Content.class);
		exp.createCriteria().andEqualTo("ifRecommend", 1);
		exp.setOrderByClause(" createtime desc ");
		Page<Object> startPage = PageHelper.startPage(1, num);
		List<Content> contentList = cm.selectByExample(exp);
		return contentList;
	}

	@Override
	public List<Integer> getContentIds(Integer channelId) {
		return cm.getIdsByChannleId(channelId);
	}

	@Override
	public List<Content> getContentList() {
		return cm.selectAll();
	}

	@Override
	public void doaddOrMinHelp(String ishelpfulOrhelpless, Integer id) {
		Content content=cm.selectByPrimaryKey(id);
		if("helpful".equals(ishelpfulOrhelpless)){
			content.setHelpful(content.getHelpful()==null?1:(content.getHelpful()+1));
		}else{
			content.setHelpless(content.getHelpless()==null?1:(content.getHelpless()+1));
		}
		cm.updateByPrimaryKeySelective(content);
	}

	@Override
	public void doaddReadCount(Integer id) {
		Content content = cm.selectByPrimaryKey(id);
		if(content != null) {
			content.setHit(content.getHit() + 1);
			cm.updateByPrimaryKeySelective(content);
		}
	}

	@Override
	public List<Content> selectContentByTitle(String content) {
		return cm.selectContentByTitle(content);
	}

	@Override
	public List<Content> selectContentByChannelCode(String code, Integer limit) {
		return cm.selectContentByChannelCode(code, limit);
	}

	@Override
	public List<Content> findHotRead() {
		return cm.findHotRead();
	}

	@Override
	public Content selectContentNext(Integer id) {
		return cm.selectContentNext(id);
	}

	@Override
	public Content selectContentPrev(Integer id) {
		return cm.selectContentPrev(id);
	}

	@Override
	public void updateHitAdd(Integer id) {
		cm.updateHitAdd(id);
	}

	@Override
	public List<Content> selectListHotChannel(String[] channelCodes) {
		List<Content> hotContents=new ArrayList<>();
		if(channelCodes.length>0){
			for (int i = 0; i < channelCodes.length; i++) {
				List<Content> contentList = cm.selectContentByChannelCode(channelCodes[i], 1);
				if(CollectionUtil.isNotEmpty(contentList)) {
					hotContents.add(contentList.get(0));
				}
			}
		}
		return hotContents;
	}

	@Override
	public List<Content> listContentBySize(int size) {
		return cm.selectContentBySize(size);
	}
}

